var vm=new Vue({
    el:"#app",
    data:{
        message:"系统公告客户端设计及实现(Bootstrap+vue+axios)",
        notices:[],
        serialnum:"序号",
        titles:"标题",
        types:"类型",
        statuss:"状态",
        createdUsers:"创建者",
        createdTime:"创建时间",
        operation:"操作",
        pageCurrent:1,
        pageCount:0,
        pre:"上一页",
        next:"下一页",
        del:"删除",
        contents:"内容",
        inform:"通知",
        announ:"公告",
        normal:"正常",
        shut:"关闭",
        update:"修改",
        id:"",
        title:"",
        content:"",
        type:1,
        status:0,
        createdUser:"",
        picked:"",
        listNoticeDiv:true,
        editNoticeDiv:false
    },
    methods:{
        doGetNotices(){
            let title=document.querySelector("#titleId").value;
            //let url=`http://localhost/notice/?pageCurrent=${this.pageCurrent}&title=${title}`;
            let url=`http://localhost/notice/`;
            axios.get(url,{
                params: {
                    pageCurrent: this.pageCurrent,//data
                    title:title
                }
            })
            .then(function (response) {
                    let jsonResult = response.data
                    console.log(jsonResult);
                    this.vm.notices=jsonResult.data.list;
                    //设置分页初始化
                    this.vm.pageCurrent=jsonResult.data.pageNum;
                    this.vm.pageCount=jsonResult.data.pages;
                })
        },
        doJumpToPage(event){//分页查询
            let cls=event.target.getAttribute("class");
            console.log("cls",cls);
            if (cls=="pre"&&this.pageCurrent>1){
                this.pageCurrent--;
            }else if (cls=="next"&&this.pageCurrent<this.pageCount){
                this.pageCurrent++;
            }else if (cls=="jump"){
                let input=document.querySelector("#pageNumId");//$("#pageNumId")
                let pageNum=input.value;//获取input对象中输入的值
                if(pageNum<1||pageNum>this.pageCount){
                    alert("页码值不正确");
                    return;
            }
                this.pageCurrent=pageNum;
                this.doGetNotices();
        }else {
               return;
            }
            this.doGetNotices();
        },
        doQueryNotices(){
            //设置当前页码值
            this.pageCurrent=1;
            //执行查询操作
            this.doGetNotices();
        },
        doDeleteById(id){
            let url=`http://localhost/notice/${id}`;
            axios.delete(url).then((response)=>{
                let result=response.data;
                if (result.state==1){
                    alert(result.message);
                    let rows=document.querySelectorAll("table>tbody>tr").length;
                    if (this.pageCurrent>1&&this.pageCurrent==this.pageCount&&rows==1){
                        this.pageCurrent--;
                    }
                    this.doGetNotices();
                }else {
                    alert(result.message);
                }
            })
        },
        doSaveOrUpdate(){
            //1.定义url
            let url="http://localhost/notice/"
            //2.定义参数
            let params={"id":this.id,"title":this.title,"content":this.content,"type":this.type,"status":this.status,"createdUser":this.createdUser}
            console.log("params",params);
            //3.发送异步请求
            axios({
                method: this.id?"put":"post",
                url: url,
                data: params
            }).then((response)=>{
                let result=response.data;
                if(result.state==1){
                    alert("save ok");
                    this.doGetNotices();
                    this.resetForm();
                    this.doCancel();
                }else{
                    alert(result.message);
                }
            })
        },
        resetForm(){
                this.id="",
                this.title="",
                this.content="",
                this.createdUser="",
                this.type=1,
                this.status=0
        },
        doFindById(id){
            let url=`http://localhost/notice/${id}`;
            axios.get(url).then((response)=>{
                let result=response.data;
                if (result.state==1){
                        this.id=result.data.id;
                        this.title=result.data.title;
                        this.content=result.data.content;
                        this.createdUser=result.data.createdUser;
                        this.type=result.data.type;
                        this.status=result.data.status;
                        this.doShowOrHide();
                }else {
                    alert(result.message);
                }
            })
        },
        doShowOrHide(){
            this.listNoticeDiv=false;
            this.editNoticeDiv=true;
        },
        doCancel(){
            this.listNoticeDiv=true,
                this.editNoticeDiv=false
        }
    },
    mounted(){
        this.doGetNotices();
    }
});